<?php
/**
 * UserController.class.php
 * 用户控制器
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-08-27
 */

namespace Attach\Controller;

class UserController extends BaseController {

    /**
     * 获取用户列表
     * @param   $p
     * @param   $condition
     * @return  $list and $page_html
     */
    function getList(){
        $condition  = $this->request('condition', []);
        $sortInfo   = $this->request('sort_info');
        $page   = $this->request('p', 1);
        $pageHtml   = true;
        $count  = true;

        if(isset($condition['father_id']) && $condition['father_id'] != ''){
            $list   = D('User')->getChildList($condition['father_id'], $condition, $sortInfo, $page, $pageHtml, $count);
        }else{
            $list   = D('User')->getList($this->userInfo, $condition, $sortInfo, $page, $pageHtml, $count);
        }
        $sql    = D('User')->getLastSql();
        $list   = D('User')->appendChildUserCount($list, true);
        $this->response([
            'list'  => $list,
            'page_html' => $pageHtml,
            'count' => $count,
            'sql'   => $sql
        ]);
    }

    /**
     * 获取VIP列表
     * @param   $p
     * @param   $sortInfo
     * @param   $condition
     * @return  $list and $page_html
     */
    function getVipList(){
        $condition  = $this->request('condition', []);
        $condition['is_vip']    = true;
        $sortInfo   = $this->request('sort_info');
        $page   = $this->request('p', 1);
        $pageHtml   = true;
        $count  = true;

        $list   = D('User')->getList($this->userInfo, $condition, $sortInfo, $page, $pageHtml, $count);

        $this->response([
            'list'  => $list,
            'page_html' => $pageHtml,
            'count' => $count
        ]);
    }

    /**
     * 获取用户详情信息
     * @param   $id
     * @return  $info
     */
    function getItem(){
        $client     = $this->unEmptyParams(['id']);
        $User   = new \Admin\Model\FUserModel();
        $this->response([
            'user_info' => $User->getUserInfo($client['id']),
            'rela_info' => $User->getRelaInfo($client['id'])
        ]);
    }

    /**
     * 更改用户信息
     * @param   $id
     * @param   $info
     * @return  boolean
     */
    function updateItem(){
        $client     = $this->unEmptyParams(['id', 'info']);
        $userId   = $client['id'];
        $userInfo   = $client['info'];
        //修改字段检测
        foreach($userInfo as $key => $val){
            if(!in_array($key, ['mobile', 'truename', 'child_school', 'child_course_name', 
                'child_name', 'child_birthday_date', 'child_city', 'child_sex', 'child_level'])){
                unset($userInfo[$key]);
            }
        }
        if(isset($userInfo['child_birthday_date']) && $userInfo['child_birthday_date'] != ''){
            $userInfo['child_birthday']     = strtotime($userInfo['child_birthday_date']);
        }
        $User   = new \Admin\Model\FUserModel();
        $oldUserInfo   = $User->getUserInfo($client['id']);

        //开启事务
        $User->startTrans();
        try {
            // //变更用户角色
            // if($userInfo['role_id'] && $userInfo['role_id'] != $oldUserInfo['role_id']){
            //     $WechatUser     = new \Wechat\Model\UserModel();
            //     $result     = $WechatUser->changeUserRole($userId, $userInfo['role_id']);
            //     if($result === false){
            //         E($WechatUser->errMsg);
            //     }
            // }

            //更改用户信息
            $result     = $User->changeUserInfo($userId, $userInfo);
            if(!$result){
                E($User->errMsg);
            }

            // //判断是否需要更改关系链
            // $relaInfo   = $User->getRelaInfo($userId);
            // if($relaInfo['father_id'] != $userInfo['father_id']){
            //     if(!$User->changeRelaInfo($userId, $userInfo['father_id'])){
            //         D($User->errMsg);
            //     }
            // }

            //提交事务
            $User->commit();
        }catch(\Exception $e){
            $User->rollback();
            $this->response($e->getMessage(), 0);
        }

        $this->response([
            'role_id'   => $userInfo['role_id'],
            'father_id' => $userInfo['father_id'],
            'mobile'    => $userInfo['mobile']
        ]);
    }

    /**
     * 导出数据
     * @param   $condition
     * @param   $sort_info
     * @param   $start
     * @param   $length
     * @return  $file
     */
    function outExcel(){
        $condition  = $this->request('condition', []);
        $sortInfo   = $this->request('sort_info');
        $start  = $this->request('start', false);
        $length     = $this->request('length', false);

        if($start === false or $length === false){
            //仅获取符合条件的数量
            $page   = false;
            $pageHtml   = false;
            $count  = true;
            D('User')->getList($this->userInfo, $condition, $sortInfo, $page, $pageHtml, $count);
            $this->response(['count' => $count]);
        }else{
            //导出数据
            $page   = [
                'start' => $start,
                'length'    => $length
            ];
            $pageHtml   = false;
            $count  = false;
            $list   = D('User')->getList($this->userInfo, $condition, $sortInfo, $page, $pageHtml, $count);

            $title  = array(
                '用户ID', '用户名', '真实姓名', '微信昵称', '手机号码', '用户角色',
                '性别', '累计金额', '钱包余额', '积分', 
                '发展人数', '国家', '省份', '城市', '学校名称', '学生数量', '教师数量',
                'VIP等级', 'VIP到期时间'
            );
            foreach($list as &$user){
                $u['id']    = $user['id'];
                $u['username']  = $user['username'];
                $u['truename']  = $user['truename'];
                $u['nickname']  = $user['nickname'];
                $u['mobile']    = $user['mobile'];
                $u['role']  = D('User')::ROLE_MAP[$user['role_id']];
                $u['sex']   = D('User')::SEX_MAP[$user['sex']];
                $u['amount']    = $user['amount'];
                $u['wallet']    = $user['wallet'];
                $u['score'] = $user['score'];
                $u['develop_count'] = $user['develop_count'];
                $u['country']   = $user['country'];
                $u['province']  = $user['province'];
                $u['city']  = $user['city'];
                $u['school_name']   = $user['school_name'];
                $u['school_students']   = $user['school_students'];
                $u['school_teachers']   = $user['school_teachers'];
                $u['vip_level'] = $user['vip_level'];
                $u['vip_enddate']   = date('Y-m-d', $user['vip_endtime']);
                $user   = $u;
            }
            $excelName  = 'user_list_'.$start;
            out_excel($title, $list, $excelName);
        }
    }

    /**
     * 导出VIP数据
     * @param   $condition
     * @param   $sort_info
     * @param   $start
     * @param   $length
     * @return  $file
     */
    function outVipExcel(){
        $condition  = $this->request('condition', []);
        $condition['is_vip']    = true;
        $sortInfo   = $this->request('sort_info');
        $start  = $this->request('start', false);
        $length     = $this->request('length', false);

        if($start === false or $length === false){
            //仅获取符合条件的数量
            $page   = false;
            $pageHtml   = false;
            $count  = true;
            D('User')->getList($this->userInfo, $condition, $sortInfo, $page, $pageHtml, $count);
            $this->response(['count' => $count]);
        }else{
            //导出数据
            $page   = [
                'start' => $start,
                'length'    => $length
            ];
            $pageHtml   = false;
            $count  = false;
            $list   = D('User')->getList($this->userInfo, $condition, $sortInfo, $page, $pageHtml, $count);

            $title  = [
                '用户ID', '微信昵称', '用户名', '真实姓名',
                'VIP等级', 'VIP到期时间', '关注时间'
            ];
            foreach($list as &$user){
                $u['id']    = $user['id'];
                $u['nickname']  = $user['nickname'];
                $u['username']  = $user['username'];
                $u['truename']  = $user['truename'];
                $u['vip_level'] = $user['vip_level'];
                $u['vip_enddate']   = date('Y-m-d', $user['vip_endtime']);
                $u['subscribe_date']    = date('Y-m-d', $user['subscribe_time']);
                $user   = $u;
            }

            out_excel($title, $list, 'vip_list_'.$start);
        }
    }
}